using System.Collections.Generic;
using MazeBuilder.Entities.Tiles;
using RoomModuleCatalog;

namespace MazeBuilder.Entities
{
  public class Maze
  {
    public Tile[][] tiles;
    public readonly IMazeConfiguration _configuration;
    public int RoomCounter { get; set; }

    public List<int> ClosedByDoors { get; set; }

    public int PlacedDoors { get; set; }

    public int MaxDoors
    {
      get { return (_configuration.MaxRoomNumber * 10) / 100; }
    }

    public Maze(IMazeConfiguration configuration) {
      _configuration = configuration;
      ClosedByDoors = new List<int>();

      tiles = new Tile[(_configuration.MaxRoomNumber +10) * _configuration.RoomHeight][];
      
      InitTilesGrid();
    }

    private void InitTilesGrid() {
      tiles.Initialize();
      for (var i = 0; i < (_configuration.MaxRoomNumber + 10) * _configuration.RoomHeight; i++) {
        tiles[i] = new Tile[(_configuration.MaxRoomNumber +10) * _configuration.RoomWidth];

        for (var j = 0; j < (_configuration.MaxRoomNumber + 10) * _configuration.RoomWidth; j++) {
          tiles[i][j] = new NullTile(i, j) {ID = -1};
        }
      }
    }

    public Tile FindTile(int row, int column)
    {
      Tile result = null;
      if (row >= 0 && row < tiles.Length && column >= 0 && column < tiles[0].Length)
        result = tiles[row][column];

      return result;
    }
  }
}